import { TokenEnums } from './enums'
export const SET_TOKEN = (token: string) => {
  localStorage.setItem(TokenEnums.TOKEN, token)
}

export const GET_TOKEN = (): string => {
  const token = localStorage.getItem(TokenEnums.TOKEN)
  return token == null ? '' : token
}

export const REMOVE_TOKEN = () => {
  localStorage.removeItem(TokenEnums.TOKEN)
}

export const SET_EXPIRED = (expireIn: number) => {
  const now = new Date()
  now.setSeconds(now.getSeconds() + expireIn)
  localStorage.setItem(TokenEnums.TOKEN_EXPIRED, now.getTime() + '')
}

export const GET_EXPIRED = () => {
  const expireIn = localStorage.getItem(TokenEnums.TOKEN_EXPIRED) as string
  return parseInt(expireIn)
}

export const HAS_EXPIRED = (): boolean => {
  return new Date().getTime() > GET_EXPIRED()
}

export const REMOVE_EXPIRED = () => {
  localStorage.removeItem(TokenEnums.TOKEN_EXPIRED)
}

export const SET_REFRESH_TOKEN = (token: string) => {
  localStorage.setItem(TokenEnums.REFRESH_TOKEN, token)
}

export const GET_REFRESH_TOKEN = (): string => {
  const token = localStorage.getItem(TokenEnums.REFRESH_TOKEN)
  return token == null ? '' : token
}

export const REMOVE_REFRESH_TOKEN = () => {
  localStorage.removeItem(TokenEnums.REFRESH_TOKEN)
}
